piping and data manipulation - {tidyverse}
geometry manipulation - {sf}, {lwgeom}, {nngeo}
network analysis - {sfnetworks}, {tidygraph}
gtfs loading - {gtfstools}
visualization - {mapview}
hebrew encoding
library(tidyverse)
library(sf)
library(lwgeom)
library(nngeo)
library(sfnetworks)
library(tidygraph)
library(gtfstools)
library(mapview)
Sys.setlocale(locale = "hebrew")
## [1] "LC_COLLATE=Hebrew_Israel.1255;LC_CTYPE=Hebrew_Israel.1255;LC_MONETARY=Hebrew_Israel.1255;LC_NUMERIC=C;LC_TIME=Hebrew_Israel.1255"
The {gtfstools} package allows for loading of an entire gtfs file. it takes a while but it is worthwhile because it is more clean.
in this stage, we perform the analysis on beersheva only
spo_gtfs <- read_gtfs("D:/Downloads/gtfs.zip",encoding = "UTF-8")
## Warning in data.table::fread(file.path(tmpdir, file_txt), select =
## fields_classes, : Found and resolved improper quoting out-of-sample. First
## healed line 50088: <<׳׳¨׳›׳– ׳©׳™׳§׳•׳ "׳׳™׳׳"/׳”׳׳“׳¢,EN,"Ilan" Rehabilitation
## Center/Hamada>>. If the fields are not quoted (e.g. field separator does not
## appear within any field), try quote="" to avoid this warning.
filter all routes operated by dan beersheva
join trips, stop times and stops coords
create a multipoint enttity
convex hull
bs_pol <- spo_gtfs$routes %>%
# at first, only for Dan Beer sheva
filter(agency_id == 32) %>%
select(route_id,route_desc) %>%
# join with trips
left_join(spo_gtfs$trips, by = "route_id") %>%
left_join(spo_gtfs$stop_times, by = "trip_id") %>%
distinct(stop_id) %>%
left_join(spo_gtfs$stops, by = "stop_id") %>%
st_as_sf(coords = c("stop_lon","stop_lat"),crs = 4326) %>%
st_transform(2039) %>%
summarise() %>%
st_convex_hull()
first, we filter only shapes that go through bs_pol we take all distinct shape_id and join them to trips,routes, and back to shapes
we create a geometry for each line makat.
we transform the geometry from wgs84 to ITM.
lines <- spo_gtfs$shapes %>%
st_as_sf(coords =c("shape_pt_lon" ,"shape_pt_lat"),crs=4326) %>%
st_transform(2039) %>%
filter(st_intersects(geometry,bs_pol,sparse = F)) %>%
st_drop_geometry() %>%
distinct(shape_id) %>%
# join with trips
left_join(spo_gtfs$trips, by = "shape_id") %>%
left_join(spo_gtfs$routes, by = "route_id") %>%
left_join(spo_gtfs$shapes, by = "shape_id") %>%
select(route_id,route_desc,shape_id,shape_pt_sequence,shape_pt_lon ,shape_pt_lat) %>%
distinct() %>%
# arrange points in each line by sequence
arrange(route_desc,shape_id,shape_pt_sequence) %>%
# create linestring for each line
group_by(route_desc,shape_id) %>%
nest() %>%
mutate(line = st_sfc(map(data, ~.x %>%
select(shape_pt_lon ,shape_pt_lat) %>%
as.matrix() %>%
st_linestring() ),crs=4326),
# transform to ITM
line = st_transform(line,2039)) %>%
select(-data) %>%
ungroup() %>%
st_sf()
m1 <- mapview(lines,zcol = "route_desc")
m1@map